package com.xsmart.oa.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author xuqinyuan
 */
@Component
public interface DistributeCountReportDao {

    /**查询分销商统计报表
     * @param page
     * @param paramMap
     * @return
     */
    @Select("<script>" +
            "select a.d_user_id,a.shop_id,a.shop_name,a.nick_name, " +
            "count(1) orderAmount,sum(a.goods_amount) totalGoodsAmount," +
            "sum(a.orderGoodsNum) totalGoodsNum,sum(a.totalCostPrice) totalCostPrice  " +
            "from (" +
            "select oi.order_id,dm.d_user_id,ss.shop_name, iu.nick_name," +
            "(case when dm.d_user_id=-1 then g.from_seller_id else ss.id end) shop_id," +
            "max(oi.goods_amount) goods_amount,count(1) orderGoodsNum," +
            "sum(og.cost_price*og.goods_number) totalCostPrice from order_info oi " +
            "left outer join infu_distributor_member dm on dm.user_id=oi.user_id " +
            "left outer join INFS_USER iu on dm.d_user_id = iu.user_id " +
            "left outer join seller_shopinfo ss on ss.ru_id=dm.d_user_id " +
            "left outer join order_goods og on og.order_id=oi.order_id " +
            "left outer join goods g on g.goods_id=og.goods_id " +
            "where 1=1 " +
            "<if test=\"nickName!=null and nickName!=''\">and iu.nick_name like concat('%',#{nickName},'%') </if> " +
            "<if test=\"orderStatus!=null and orderStatus!=''\">and oi.order_status = #{orderStatus} </if> " +
            "<if test=\"shippingStatus!=null and shippingStatus!=''\">and oi.shipping_status = #{shippingStatus} </if> " +
            "<if test=\"payStatus!=null and payStatus!=''\">and oi.pay_status = #{payStatus} </if> " +
            "<if test=\"addStartTime!=null and addStartTime!=''\">and oi.add_time &gt; #{addStartTime} </if> " +
            "<if test=\"addEndTime!=null and addEndTime!=''\">and oi.add_time &lt; #{addEndTime} </if> " +
            "<choose> " +
            "<when test='timeType==1'>and to_days(oi.add_time) = to_days(now())</when>" +
            "<when test='timeType==2'>and (DATE_SUB(CURDATE(), INTERVAL 3 DAY) &lt; date(oi.add_time) or to_days(oi.add_time) = to_days(now()))</when>" +
            "<when test='timeType==3'>and YEARWEEK(date_format(oi.add_time,'%Y-%m-%d')) = YEARWEEK(now())</when>" +
            "<when test='timeType==4'>and DATE_FORMAT(oi.add_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )</when>" +
            "<when test='timeType==5'>and (PERIOD_DIFF(date_format( now() , '%Y%m' ) , date_format(oi.add_time, '%Y%m')) in (0,1,2) " +
            ")</when>" +
            "</choose>" +
            "group by oi.order_id,dm.d_user_id,ss.id " +
            ") a " +
            "group by a.d_user_id,a.shop_id " +
            "order by a.nick_name ASC   " +
            "</script>")
    List<Map<String,Object>> queryCountReport(Pagination page,Map paramMap);


    /**查询分销商报表销售商品清单
     * @param page
     * @param paramMap
     * @return
     */

    @Select("<script>" +
            "select oi.order_id,dm.d_user_id,ss.shop_name, iu.nick_name,og.goods_id,og.cost_price,og.goods_name,\n" +
            "(case when dm.d_user_id=-1 then g.from_seller_id else ss.id end) shop_id,\n" +
            "goods_amount,og.goods_number  \n" +
            "from order_info oi \n" +
            "left outer join infu_distributor_member dm on dm.user_id=oi.user_id \n" +
            "left outer join INFS_USER iu on dm.d_user_id = iu.user_id \n" +
            "left outer join seller_shopinfo ss on ss.ru_id=dm.d_user_id \n" +
            "left outer join order_goods og on og.order_id=oi.order_id \n" +
            "left outer join goods g on g.goods_id=og.goods_id \n" +
            "where 1=1 " +
            "and ss.id = #{shopId} " +
            "<choose> " +
            "<when test='timeType==1'>and to_days(oi.add_time) = to_days(now())</when>" +
            "<when test='timeType==2'>and (DATE_SUB(CURDATE(), INTERVAL 3 DAY) &lt; date(oi.add_time) or to_days(oi.add_time) = to_days(now()))</when>" +
            "<when test='timeType==3'>and YEARWEEK(date_format(oi.add_time,'%Y-%m-%d')) = YEARWEEK(now())</when>" +
            "<when test='timeType==4'>and DATE_FORMAT(oi.add_time, '%Y%m' ) = DATE_FORMAT( CURDATE( ) , '%Y%m' )</when>" +
            "<when test='timeType==5'>and (PERIOD_DIFF(date_format( now() , '%Y%m' ) , date_format(oi.add_time, '%Y%m')) in (0,1,2) " +
            ")" +
            "</when>" +
            "</choose>" +
            "ORDER BY oi.order_id,og.goods_id\n" +
            "</script>")
    List<Map<String,Object>> queryCountReportGoodsList(Pagination page,Map paramMap);
}
